<?php

namespace Admin\Controller;
use Think\Controller;
class OrderController extends AdminController {
	// 会员查询操作
	public function orderlist()
	{

    // 获取排序条件
    $content = null   != (string)(I('request.sort')) ? (string)(I('request.sort')) : 'desc';
    $order['id'] = $content;

		// 获取搜索条件
		$order_num         =   I('order_num');
    $map['status']     =   array('egt',0);
    if(is_numeric($order_num)){
        $map['a.id|a.user_id|a.order_num']=   array(intval($order_num),array('like','%'.$order_num.'%'),'_multi'=>true);
    }else{
        $map['a.order_num']    =   array('like', '%'.(string)$order_num.'%');
    }

    // 计算订单列表的总数
    $total            = D('order')->alias('a')->where($map)->count();

    // 获取每页显示的条数
    $listRows         = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;

    // 实例化分页类，传入总数和每页显示条数
    $page = new \Think\Page($total, $listRows);

    $list = D('order')->alias('a')->join('north_user_addr b ON a.addr_id=b.addr_id')->limit($page->firstRow.','.$page->listRows)->where($map)->order($order)->select();

		$stat = ['未付款','已取消','已付款','已发货','交易完成','已评价','申请退货','退货完成','拒绝退单'];
   	foreach($list as $key=>&$val){
    	$val['order_status'] = $stat[$val['order_status']];
    	$val['shop_time'] = date('Y/m/d',$val['shop_time']);
    }
		$this->assign('list',$list);
		// 如果查询总数大于每页显示的条数，则显示分页
    if($total>$listRows){
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
    }
    $p   = $page->show();
    $this->assign('_page', $p? $p: '');
    $this->assign('list',$list);
    $this->display();
	}

	// 订单ID排序操作
	public function ordersort()
	{
		$order = D('order');
		$list=$order->myordersort();
		$stat = ['未付款','已取消','已付款','已发货','交易完成','已评价','申请退货','退货完成','拒绝退单'];
		foreach($list as $key=>&$val){
			$val['order_status'] = $stat[$val['order_status']];
		}
		//  // 变量赋值
		$this->assign('list',$list);
		// 模板展示
		$this->display('orderlist');
	}

	/**
	 * 订单详情的查询与展示
	 */
	public function orderDetail()
	{
    // 获取排序条件
    $content = null != (string)(I('request.sort')) ? (string)(I('request.sort')) : 'desc';
    $order['id'] = $content;

    if(null !== ($id = I('get.id'))){
      if(is_numeric($id)){
        $map['id'] = $id;
      }
    }
		// 获取搜索条件
		$order_num       =   I('order_num');
    $map['status']   =   array('egt',0);
    if(is_numeric($order_num)){
        $map['user_id|order_num|goods_name']=   array(intval($order_num),array('like','%'.$order_num.'%'),'_multi'=>true);
    }else{
        $map['order_num|goods_name']    =   array('like', '%'.(string)$order_num.'%');
    }

		$list = $this->lists('order_detail',$map,$order);
	     // 变量赋值
		$this->assign('list',$list);
		// 模板展示
		$this->display();
	}

	/**
	 * 订单详情排序
	 */
	public function orderDetailsort()
	{
		$order_detail= new \Admin\Model\OrderModel('order_detail');
		$list=$order_detail->myorderDetailsort();
		//  // 变量赋值
		$this->assign('list',$list);
		// 模板展示
		$this->display('orderDetail');
	}
	/**
	 * 订单退货展示与查询
	 */
	public function orderReturn()
	{		

    // 获取排序条件
    $content = null != (string)(I('request.sort')) ? (string)(I('request.sort')) : 'desc';
    $order['id'] = $content;
			
		// 获取搜索条件
		$order_num       =   I('order_num');
    $map['status']   =   array('egt',0);
    if(is_numeric($order_num)){
        $map['username|order_num']    =   array(intval($order_num),array('like','%'.$order_num.'%'),'_multi'=>true);
    }else{
        $map['username|order_num']    =   array('like', '%'.(string)$order_num.'%');
    }
    $list = $this->lists('order_return',$map,$order);

    $status = ['未付款','已取消','已付款','已发货','交易完成','已评价','申请退货','退货完成','拒绝退单'];
    foreach($list as &$val){
      $val['order_status'] = $status[$val['order_status']];
    }
    // print_r($list);exit;

		$this->assign('list',$list);
		$this->display();
	}
	/**
   * 退货订单处理意见
   */
  public function dealwith()
  {
    $order_num=I('get.order_num');
    // echo $order_num;exit;
    $order_return= new \Admin\Model\OrderModel('order_return');
    $map['order_num']=['eq',$order_num];
    $list=$order_return->where($map)->find();
    // echo D()->getLastSql();exit;
    $this->assign('list',$list);
    $this->display();
  }

	/**
   * 退货订单的提交和处理更新
   */
  public function returnaction(){
        $order_return= new \Admin\Model\OrderModel('order_return');
        $data = $order_return->create(I('post.'));
        $data['handletime'] = time();
        $data['handlename']=$_SESSION['onethink_admin']['user_auth']['username'];

        $order_num=I('post.order_num');
        $order=new \Home\Model\PersonCenterModel("order");
        $map["order_num"]=["eq",$order_num];
        $orderlist=$order->where($map)->find();
        // 分为同意退单和拒绝退单两种选择
        if(I("post.result")){
              $data["order_status"]=7;
              $res=$order_return->save($data);
              // 订单处理完毕后，订单状态改为退货完成
              $orderlist["order_status"]=7;
              $orderres=$order->save($orderlist);
        }else{
              $data["order_status"]=8;
              $res=$order_return->save($data);

              $orderlist["order_status"]=8;
              $orderres=$order->save($orderlist);
        }
        if($res&&$orderres){
          $this->success('退货处理成功',U('orderReturn'));
        }else{
          $this->error('退货处理失败',U('orderReturn'));
        }
  }

  // 订单发货
  public function delivery(){
    $order_id=I('get.id');
    $order=new \Home\Model\PersonCenterModel("order");
    $orderlist=$order->find($order_id);
    if($orderlist['order_status']==2){
      $orderlist['order_status']=3;
    }
    $res=$order->save($orderlist);
    // 发货成功后的跳转
    if($res){
      $this->success('发货成功',U('orderlist'));
    }else{
      $this->error('发货失败',U('orderlist'));
    }
  }



}
